home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / lzw4w10.zip / RW_IO.C < prev    next >
Text File  |  1994-07-08  |  3KB  |  113 lines

  1. /*   RW_IO.C
  2. **
  3. **   Reader/Writer Buffered I/O
  4. **
  5. **   Reader() and Writer() are called directly by the LZW4C.ASM code.
  6. **   They should never be called by your application code.
  7. **
  8. **   The other functions are never called by the LZW4C.ASM code, but are
  9. *    called only by your application routines.
  10. **
  11. **   Note that only a Reader() and Writer() function is required by the
  12. **   LZW4C.ASM code. This means that you have complete control over data
  13. **   coming into and out of the compression/expansion code. Instead of
  14. **   reading or writing to disk, you can just as easily read/write to a
  15. **   buffer, serial port, etc. You just have to write the Reader() and
  16. **   Writer() code.
  17. */
  18.  
  19. #include <windows.h>
  20. #include <stdio.h>
  21. #include "rw_io.h"
  22. #include "display.h"
  23.  
  24. #define BUFFER_SIZE 2048
  25.  
  26. typedef struct IOstruct
  27. {char Buffer[BUFFER_SIZE];
  28.  int  Left;      /* leftmost byte in Buffer */
  29.  int  Right;     /* rightmost byte in buffer */
  30.  long Count;     /* # times Reader/Writer called */
  31.  HANDLE Handle;
  32.  OFSTRUCT ofs;
  33. } IOstruct;
  34.  
  35. static HWND hTheWnd = 0;
  36. static IOstruct InpControl;
  37. static IOstruct OutControl;
  38. static int X = 1;
  39. static int Y = 15;
  40.  
  41. int FAR PASCAL ReaderOpen(LPSTR Ptr)
  42. {int Code;
  43.  /* open input file */
  44.  InpControl.Left = 0;
  45.  InpControl.Right = 0;
  46.  InpControl.Count = 0;
  47.  Code = OpenFile(Ptr,&InpControl.ofs,OF_READ);
  48.  if(Code==-1) return(FALSE);
  49.  InpControl.Handle = (HANDLE)Code;
  50.  return(TRUE);
  51. }
  52.  
  53. int FAR PASCAL Reader(void)
  54. {char Byte;
  55.  if(InpControl.Left==InpControl.Right)
  56.     {/* read next buffer */
  57.      InpControl.Left = 0;
  58.      InpControl.Right = _lread(InpControl.Handle,&InpControl.Buffer,BUFFER_SIZE);
  59.      if(InpControl.Right<=0) return(EOF);
  60.     }
  61.  /* return next byte */
  62.  Byte = InpControl.Buffer[InpControl.Left++];
  63.  InpControl.Count++;
  64.  return(0x00ff&Byte);
  65. }
  66.  
  67. long FAR PASCAL ReaderCount(void)
  68. {/* return bytes read */
  69.  return(InpControl.Count);
  70. }
  71.  
  72. void FAR PASCAL ReaderClose(void)
  73. {/* close input file */
  74.  _lclose(InpControl.Handle);
  75. }
  76.  
  77. int FAR PASCAL WriterOpen(LPSTR Ptr)
  78. {int Code;
  79.  /* open output file */
  80.  OutControl.Left = 0;
  81.  OutControl.Right = 0;
  82.  OutControl.Count = 0;
  83.  Code = OpenFile(Ptr,&OutControl.ofs,OF_CREATE);
  84.  if(Code==-1) return(FALSE);
  85.  OutControl.Handle = (HANDLE)Code;
  86.  return(TRUE);
  87. }
  88.  
  89. int FAR PASCAL Writer(char Byte)
  90. {int Code;
  91.  HDC hDC;
  92.  OutControl.Count++;
  93.  if((OutControl.Count&0x0fff)==0) DisplayText(".");
  94.  OutControl.Buffer[OutControl.Right++] = Byte;
  95.  if(OutControl.Right==BUFFER_SIZE)
  96.     {/* read next buffer */
  97.      Code = _lwrite(OutControl.Handle,&OutControl.Buffer,OutControl.Right);
  98.      OutControl.Right = 0;
  99.     }
  100.  return(Code);
  101. }
  102.  
  103. long FAR PASCAL WriterCount(void)
  104. {/* return bytes written */
  105.  return(OutControl.Count);
  106. }
  107.  
  108. void FAR PASCAL WriterClose(void)
  109. {/* flush buffer to disk */
  110.  _lwrite(OutControl.Handle,&OutControl.Buffer[OutControl.Left],OutControl.Right-OutControl.Left);
  111.  /* close output file */
  112.  _lclose(OutControl.Handle);
  113. }